package com.xms.core.data;

import com.xms.utils.StringUtil;
import org.joda.time.DateTime;

import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 * 实体数据对象
 * </p>
 *
 * @author migo
 * @since 2021/9/1
 */
public class Entity extends HashMap<String, Object> {
    public Entity(String name, HashMap<String, Object> map)
    {
        super(map);
        this.Name = name;
    }

    public Entity(String name)
    {
        this.Name = name;
    }

    private UUID Id;

    public String Name;
    private String _idName;
    public Entity setIdName(String name){
        _idName = name;
        return this;
    }
    public String getIdName()
    {
        return StringUtil.isEmpty(_idName) ? Name + "id" : _idName;
    }

    public Entity addIfNotContain(String key, Object value)
    {
        if (!this.containsKey(key))
        {
            this.setItem(key, value);
        }
        return this;
    }

    public Entity setIdValue(UUID value, String idName)
    {
        if (StringUtil.notEmpty(idName))
        {
            this.setIdName(idName);
        }
        this.setIdValue(value);
        return this;
    }

    public Entity setIdValue(UUID value)
    {
        this.setItem(this.getIdName(), value);
        Id = value;
        return this;
    }

    public UUID getIdValue()
    {
        if (this.containsKey(this.getIdName()))
        {
            Object id = this.get(this.getIdName());
            if(id != null){
                Id = UUID.fromString(id.toString());
            }
        }
        return Id;
    }

    public Entity removeItems(String... keys){
        if(keys != null && keys.length > 0){
            for (String k : keys){
                if(super.containsKey(k)) {
                    super.remove(k);
                }
            }
        }
        return this;
    }

    public Entity removeItems(Collection<String> keys){
        if(keys != null && keys.size() > 0){
            for (String k : keys){
                if(super.containsKey(k)) {
                    super.remove(k);
                }
            }
        }
        return this;
    }

    public Entity setValue(String key, Object value)
    {
        this.setItem(key, value);
        return this;
    }

    private Entity setItem(String key, Object value){
        if (this.containsKey(key))
        {
            this.replace(key, value);
        }
        else{
            this.put(key, value);
        }
        return this;
    }

    public Object getValue(String key){
        if(this.containsKey(key)){
            return this.get(key);
        }
        return null;
    }

    public String getStringValue(String key){
        if(this.containsKey(key)){
            Object val = this.get(key);
            return val != null ? val.toString() : "";
        }
        return "";
    }

    /**
     *
     * @param key
     * @return 如果不存在则返回 -1
     */
    public int getIntValue(String key){
        return this.getIntValue(key, -1);
    }

    public int getIntValue(String key, int defaultValueIfNull){
        if(!this.containsKey(key)){
            return defaultValueIfNull;
        }
        Object val = this.getValue(key);
        if(val == null)
            return defaultValueIfNull;
        return Integer.parseInt(String.valueOf(val));
    }

    public float getFloatValue(String key){
        return Float.parseFloat(this.getStringValue(key));
    }

    public boolean getBoolValue(String key){
        String val = this.getStringValue(key);
        if(val.isEmpty()) return false;
        return Boolean.parseBoolean(val);
    }

    public UUID getUUIDValue(String key){
        Object id = this.getValue(key);
        if(id != null)
            return UUID.fromString(id.toString());
        return null;
    }

    public DateTime getDateValue(String key){
        String val = this.getStringValue(key);
        if(val.isEmpty()) return null;
        return DateTime.parse(this.getStringValue(key));
    }
}
